package com.kimehwa.component;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * @author xiaodongguan
 * @description
 * @since 2021-08-14 18:11
 */
@Slf4j
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {

        // 可以判断数据表中是否有这个列名，如果没有不会调用，不会让程序产生开销
        boolean createTime = metaObject.hasSetter("createTime");
        if (createTime) {
            log.info("insert filled");
            System.out.println("insert filled");
            setInsertFieldValByName("createTime", LocalDateTime.now(), metaObject);
        }

    }

    @Override
    public void updateFill(MetaObject metaObject) {
        Object updateTime = getFieldValByName("updateTime", metaObject);
        if (updateTime == null) {
            log.info("update filled");
            setUpdateFieldValByName("updateTime", LocalDateTime.now(), metaObject);
        }

    }
}
